package cn.socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * Desc : TODO
 * ClassName : cn.socket.ClientSocket
 *
 * @author : 林鹏群
 * @date : 2022/12/22 19:54
 */
public class ClientQQ {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8899);
        Scanner scanner = new Scanner(System.in);

        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()) {
            while (true) {
                // 1. 从控制台读取数据, 构造成一个 请求
                System.out.print("-> ");
                String request = scanner.next();
                // 2. 发送请求给服务器
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.println(request);
                // 这个 flush 不要忘记. 否则可能导致请求没有真发出去.
                printWriter.flush();
                // 3. 从服务器读取响应
                Scanner respScanner = new Scanner(inputStream);
                String response = respScanner.next();
                // 4. 把响应显示到界面上
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
